package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.TaskUserDao;
import cn.topevery.garden.entity.po.TaskUser;
import cn.topevery.garden.entity.query.TaskUserQuery;
import cn.topevery.garden.entity.vo.TaskUserVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 检查任务-考评人员关联
 */
@Repository
public class TaskUserDaoImpl extends BaseDaoImpl<TaskUser> implements TaskUserDao {
    @Override
    public <P extends DbCsp> List<TaskUser> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<TaskUserVO> listPage(TaskUserQuery query) {
        String table = "t_task_user ttu";

        String columns = "ttu.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttu.c_id desc");
        sqlQueryMate
                .where(true, "ttu.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, TaskUserVO.class);
    }

    @Override
    public TaskUser getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    /**
     * 根据业务类型，任务ID或考评用户ID 获取考评任务-用户关联列表
     *
     * @param taskType         业务类型
     * @param taskId           任务ID
     * @param evaluationUserId 考评用户ID
     * @return 考评任务-用户关联列表
     */
    @Override
    public List<TaskUserVO> listByTaskTypeOrId(Integer taskType, Integer taskId, Integer evaluationUserId) {
        String columns = "ttu.*";
        String table = "t_task_user ttu";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "ttu.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(taskType != null, "ttu.c_task_type", ScriptConditionEnum.Eq, taskType)
                .where(taskId != null, "ttu.c_task_id", ScriptConditionEnum.Eq, taskId)
                .where(evaluationUserId != null, "ttu.c_evaluation_user_id", ScriptConditionEnum.Eq, evaluationUserId)
        ;
        return selectList(sqlQueryMate, TaskUserVO.class);
    }
}
